<?php


namespace app\admin\controller;


class AuthRule extends \app\index\controller\Common
{
    protected $beforeActionList = [
        'beforeDel' => ['only'=>'delete']
    ];
    public function lst(){
        if(request()->isPost()){
            $sort = input('post.');
            $this->sort($sort);
        }
        $authRuleRes = db('auth_rule')->order('sort desc')->select();
        //无限级分类
        $AuthRule = new \app\admin\model\AuthRule();
        $authRuleRes =  $AuthRule->ulCf($authRuleRes);
        $this->assign('authRuleRes',$authRuleRes);
        return view('list');
    }
    public function sort($sort){
        //批量更新排序数据
        $AuthRule = new \app\admin\model\AuthRule();
        foreach($sort as $k=>$v){
            $AuthRule->save([
                'sort'=>$v
            ],['id'=>$k]);
        }
            $this->success("更新排序成功",url('lst'));
    }
    public function add(){
        if(request()->isPost()){
            $data = input('post.');
            $plevel = db('auth_rule')->where('id',$data['pid'])->field('level')->find();
            if($plevel){
                $data['level'] = $plevel['level']+1;
            }else{
                $data['level'] = 0;
            }
            $add = db('auth_rule')->insert($data);
            if($add){
                $this->success('权限添加成功',url('lst'));
            }else{
                $this->error('权限添加失败',url('lst'));
            }
        }
        $authrules = db('auth_rule')->order('sort desc')->select();
        $AuthRule = new \app\admin\model\AuthRule();
        $authRuleRes =  $AuthRule->ulCf($authrules);
        $this->assign('authRuleRes',$authRuleRes);
        return view();
    }
    public function edit(){
        if(request()->isPost()){
            $data= input('post.');
            $plevel = db('auth_rule')->where('id',$data['pid'])->field('level')->find();
            if($plevel){
                $data['level'] = $plevel['level']+1;
            }else{
                $data['level'] = 0;
            }
           if(!array_key_exists('status', $data)){
               $data['status']=0;
           }
           if(db('auth_rule')->update($data)){
               $this->success('权限修改成功',url('lst'));
           }else{
               $this->error('权限修改失败',url('lst'));
           }
        }
        $id = input('id');
        $rule = db('auth_rule')->where('id',$id)->find();
        $authrules = db('auth_rule')->order('sort desc')->select();
        $AuthRule = new \app\admin\model\AuthRule();
        $authRuleRes =  $AuthRule->ulCf($authrules);
        $this->assign('authRuleRes',$authRuleRes);
        $this->assign('rule',$rule);
        return view();
    }
    public function delete(){
        $id = input('id');
        if(db('auth_rule')->where('id',$id)->delete()){
            $this->success('权限及其子权限删除成功',url('lst'));
        }else{
            $this->error('权限删除失败',url('lst'));
        };
    }
    public function beforeDel(){
        $id = input('id');
        $authRuleRes = db('auth_rule')->field('id,pid')->order('id desc')->select();
        $sonid = $this->getChildrenId($authRuleRes,$id);
        //删除 子权限
        db('auth_rule')->delete($sonid);
    }
    public function getChildrenId($data,$id){
        static $sonid = array();
        foreach($data as $k=>$v){
            if($v['pid']==$id){
               $sonid[] = $v['id'];
               $this->getChildrenId($data,$v['id']);
            }
        }
        return $sonid;
    }

}